<?php
declare (strict_types = 1);

namespace app\api\controller;
use app\model\CategoryModel;
use app\model\CourseMenusModel;
use app\model\CourseModel;
use app\model\MenusModel;
use think\App;

class Courses extends Base {

    private $courseModel;

    private $req;

    private $page;

    private $limit = 20;

    private $sortType;

    private $course_menus_id;

    private $orderConfigList = [
        "hot"=>["field"=>"c.view_num","order"=>"desc"],
        "new"=>["field"=>"c.created_at","order"=>"desc"],
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->req = request()->param();

        $this->page = isset($this->req['page']) ? $this->req['page']  : 1;

        $this->sortType = (isset($this->req['sortType']) && !empty($this->req['sortType'])) ? $this->req['sortType'] : "new";

        $this->course_menus_id = isset($this->req['course_menus_id']) ?  $this->req['course_menus_id'] : "";

        $this->courseModel = new CourseModel();
    }



    public function getList()
    {

        $orderSelect = $this->orderConfigList[$this->sortType];
        $orderSelect['order'] = isset($this->req['order']) ? $this->req['order'] : $orderSelect["order"];

        $where =[];
        $where[] = ['c.is_delete',"=",0];
        $where[] = ['c.course_status','=',1];
        $where[] = ['m.is_delete',"=",0];
        $where[] = ['m.course_menus_status','=',1];

        if(isset($this->course_menus_id) && !empty($this->course_menus_id)){
            $where[] = ['c.course_menus_id','=',$this->course_menus_id];
        }

        if(!empty($this->req['keyword'])){
            $where[] = ['c.course_name|c.teacher|m.course_menus_name','like',"%".$this->req['keyword']."%"];
        }

        $list = $this->courseModel->getList($where,$this->page,$this->limit,$orderSelect["field"],$orderSelect["order"]);

        $total = $this->courseModel->getTotal();

        return $this->succData($total,$list);
    }


    public function getInfo()
    {
        $course_id = isset($this->req['course_id']) ? $this->req['course_id'] : "";

        if(empty($course_id)){
            return $this->fail("请选择课程");
        }

        $info = $this->courseModel->getInfo($course_id);

        if(empty($info)){
            return $this->fail("课程不存在");
        }

        return $this->succ($info);
    }




}
